package service.report.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import model.contract.Loangrantrecord;

import dao.LoangrantDao;
import dao.LoangrantrecordDao;
import dao.TContractDao;

import service.report.ReportDetailService;
import util.MoneyCalc;

public class ReportDetailServiceImpl implements ReportDetailService {
	private TContractDao tContractDao;
	private LoangrantDao loangrantDao;
	private LoangrantrecordDao loangrantrecordDao;
	
	@Override
	public List<?> getReportDetailList() {
		StringBuffer select=new StringBuffer().append(" ( case  when tc.clienttype = '2' then ( select c.name from companybasic as c where c.id = tc.clientid limit 0,1) ")
				.append(" when tc.clienttype = '1' then ( select p.name from personalbasic as p where p.id = tc.clientid limit 0,1) ")
				.append(" else '0' end ) as customname, ")
				.append(" ( case when tc.clienttype = '2' then ( select cc.loanuse from creditapplyenterprise as cc where cc.applyid = tc.applyid limit 0,1) ")
				.append(" when tc.clienttype = '1' then ( select cp.loanuse from creditapplypersonal as cp where cp.applyid = tc.applyid limit 0,1) ")
				.append(" else '0' end) as loanuse, ")
				
				.append(" ( case  when tc.clienttype = '2' then  ( select cc.applytime from creditapplyenterprise as cc where cc.applyid = tc.applyid limit 0,1) ")
				.append(" when tc.clienttype = '1' then ( select cp.applytime from creditapplypersonal as cp where cp.applyid = tc.applyid limit 0,1) ")
				.append(" else '0' end) as applytime, ")
				
				.append(" ( case  when tc.clienttype = '2' then  ( select cc.applytimetype from creditapplyenterprise as cc where cc.applyid = tc.applyid limit 0,1)  ")
				.append(" when tc.clienttype = '1' then ( select cp.applytimetype from creditapplypersonal as cp where cp.applyid = tc.applyid limit 0,1) ")
				.append(" else '0' end) as applytimetype, ")
				
				.append("tc.startdate, ")
				.append("tc.enddate, ")
				.append("tc.performrates, ")
				.append("tc.isaboutn, ")
				.append("tc.clienttype, ")
				.append("tc.id, ")
				.append("tc.modeD, tc.modeZ, tc.modeB, tc.modeX");
		//StringBuffer from=new StringBuffer().append("loangrant as l, t_contract as tc where (tc.contractstate = 1 or tc.contractstate = 0) and (l.state = 0 or l.state = 2 or l.state = 3) and l.contractid = tc.id ");
		StringBuffer from=new StringBuffer().append("t_contract as tc where (tc.contractstate = 1 or tc.contractstate = 0) and tc.id in (select DISTINCT l.contractid from loangrant as l where (l.state = 0 or l.state = 2 or l.state = 3))");
		List<?> listBean = tContractDao.findSqlQuery(select.toString(), from.toString(), 0);
		List<Map<Object,Object>> list = new ArrayList<Map<Object,Object>>();
		if(listBean != null){
			for(int i = 0, j = listBean.size(); i < j; i++){
				@SuppressWarnings("unchecked")
				Map<Object,Object> mapbean = (Map<Object, Object>)listBean.get(i);
				int loanid = (Integer)mapbean.get("Id");
				mapbean.put("loanid", loanid);
				List<Loangrantrecord> loanList = this.loangrantrecordDao.findModel("contractid = " + loanid + " and state = 0 order by repaydate desc");
				//设置还款日
				if(loanList != null){
					try {
						Timestamp tsp = loanList.get(0).getRepaydate();
						//Timestamp tsp = this.loangrantrecordDao.findModel("contractid = " + loanid + " order by repaydate desc").get(0).getRepaydate();
						//this.
						//Timestamp tsp = this.loangrantDao.findModel("contractid = " + loanid + " order by repaydate desc").get(0).getRepaydate();
						mapbean.put("giveTime", tsp);
					} catch (Exception e) {
						mapbean.put("giveTime", null);
					}
				}else{
					mapbean.put("giveTime", null);
				}
				List listApplyMoney = this.loangrantDao.findQuery("new vo.MoneyDoubleBean(l.grantmoney)", "Loangrant as l where l.contractid = " + loanid + " and (l.state = 2 or l.state = 3 or l.state = 0)");
				List listbalance = this.loangrantDao.findQuery("new vo.MoneyDoubleBean(l.balance)", "Loangrant as l where l.contractid = " + loanid + " and (l.state = 2 or l.state = 3 or l.state = 0)");
				BigDecimal allBalance = MoneyCalc.moneyTotal(listbalance).setScale(2, BigDecimal.ROUND_HALF_UP);
				BigDecimal allApplyMoney = MoneyCalc.moneyTotal(listApplyMoney).setScale(2, BigDecimal.ROUND_HALF_UP);
				//设置余额显示
				mapbean.put("balanceMoney", allBalance);
				mapbean.put("applymoney", allApplyMoney);
				
				//设定前台显示担保方式
				String loanofmode = " ";
				try {
					int moded = (Integer)mapbean.get("modeD");
					if(moded == 1){
						loanofmode += "抵押 ";
					}
				} catch (Exception e) {
					
				}
				try {
					int modez = (Integer)mapbean.get("modeZ");
					if(modez == 1){
						loanofmode += "质押 ";
					}
				} catch (Exception e) {
					
				}
				try {
					int modeb = (Integer)mapbean.get("modeB");
					if(modeb == 1){
						loanofmode += "保证 ";
					}
				} catch (Exception e) {
					
				}
				try {
					int modex = (Integer)mapbean.get("modeX");
					if(modex == 1){
						loanofmode += "信用 ";
					}
				} catch (Exception e) {
					
				}
				
				mapbean.put("loanofmode", loanofmode);
				
				//设置是否涉农
				String isAboutN = " ";
				try {
					int isabn = (Integer)mapbean.get("isaboutn");
					if(isabn == 1){
						isAboutN = "是";
						try {
							BigDecimal applyaboutMoney = (BigDecimal)mapbean.get("applymoney");
							mapbean.put("applyaboutMoney", applyaboutMoney);
						} catch (Exception e) {
							mapbean.put("applyaboutMoney", 0.00);
						}
						mapbean.put("balanceaboutMoney", allBalance);
					}else{
						mapbean.put("isAboutN", " ");
						mapbean.put("applyaboutMoney", 0.00);
						mapbean.put("balanceaboutMoney", 0.00);
					}
				} catch (Exception e) {
					
				}
				mapbean.put("isAboutN", isAboutN);
				
				String clientType = " ";
				
				try {
					char ctype = (Character)mapbean.get("clienttype");
					if(ctype == '1'){
						clientType = "个人";
					}else if(ctype == '2'){
						clientType = "企业";
					}else{
						
					}
				} catch (Exception e) {
					//e.printStackTrace();
				}
				
				mapbean.put("clientType", clientType);
				
				try {
					BigDecimal money = (BigDecimal)mapbean.get("applymoney");
					mapbean.put("money", money);
				} catch (Exception e) {
					mapbean.put("money", 0.00);
				}
				mapbean.put("bMoney", allBalance);
				list.add(mapbean);
			}
		}
		return list;
	}
	
	public List<?> getLoanInfor(int inforLoanid){
		List<?> list = this.loangrantDao.findModel("contractid = " + inforLoanid + " and (state = 3 or state = 2 or state = 0)");
		return list;
	}
	
	public List<?> getBackInfor(int inforLoanid){
		List<?> list = this.loangrantrecordDao.findModel("contractid = " + inforLoanid + " and state = 0");
		return list;
	}
	
	public TContractDao gettContractDao() {
		return tContractDao;
	}

	public void settContractDao(TContractDao tContractDao) {
		this.tContractDao = tContractDao;
	}

	public LoangrantDao getLoangrantDao() {
		return loangrantDao;
	}

	public void setLoangrantDao(LoangrantDao loangrantDao) {
		this.loangrantDao = loangrantDao;
	}

	public LoangrantrecordDao getLoangrantrecordDao() {
		return loangrantrecordDao;
	}

	public void setLoangrantrecordDao(LoangrantrecordDao loangrantrecordDao) {
		this.loangrantrecordDao = loangrantrecordDao;
	}

	@Override
	public String getClientInforByLoanid(int inforLoanid) {
		StringBuffer select=new StringBuffer().append("( case when tc.clienttype = '2' then ( select c.name from companybasic as c where c.id = tc.clientid limit 0,1) when tc.clienttype = '1' then ( select p.name from personalbasic as p where p.id = tc.clientid limit 0,1) else '0' end ) as customname ");
		StringBuffer from=new StringBuffer().append("t_contract as tc where (tc.contractstate = 1 or tc.contractstate = 0) and tc.id = " + inforLoanid);
		List<?> listBean = tContractDao.findSqlQuery(select.toString(), from.toString(), 0);
		if(listBean != null){
			@SuppressWarnings("unchecked")
			Map<Object,Object> mapbean = (Map<Object, Object>)listBean.get(0);
			return mapbean.get("customname").toString();
		}
		return null;
	}

}
